import pymysql

from Common.handle_config import conf

class HandleMysql():
    def __init__(self):
        self.conn = pymysql.connect(
            host = conf.get("MYSQL","host"),
            port = int(conf.get("MYSQL","port")),
            user=conf.get("MYSQL", "user"),
            password=conf.get("MYSQL", "password"),
            database=conf.get("MYSQL", "database")
        )

        self.cur =self.conn.cursor()

    def select_one_data(self,sql):
        self.conn.commit()
        self.cur.execute(sql)
        return self.cur.fetchone()

    def select_all_data(self,sql):
        self.conn.commit()
        self.cur.execute(sql)
        return self.cur.fetchall()

    def get_count(self,sql):
        self.conn.commit()
        return self.cur.execute(sql)

    def cloce(self):
        self.conn.close()
        self.cur.close()

db = HandleMysql()

if __name__ == '__main__':
    sql = 'SELECT * FROM `user` WHERE userPhone = "15237531610"'
    sql1 = 'SELECT * FROM `user` LIMIT 10'
    db = HandleMysql()
    data = db.select_one_data(sql)
    print("获取一条数据",data)
    data = db.select_all_data(sql1)
    print("获取全部数据",data)
    data = db.get_count(sql)
    print("获取条数",data)